layui.extend({
    baseConfig : "/js/pages/baseConfig"
})
layui.use(['form','layer','laydate','table','laytpl','element','util','baseConfig'],function(){

    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        baseConfig = layui.baseConfig,
        table = layui.table;
    var util = layui.util;
    var tableIns = table.render({
        elem: '#list',
        url : '/cooperative/list/',
        text: { none: "Aucun résultat"},
        cellMinWidth : 95,
        height : "full-125",
        id : "listTable",
        cols : [[
            {field: 'type', title: "合作商类型", align:"center", templet : function( d){
                switch ( d.type) {
                    case 1:return "Fourenisseur";
                    case 2:return "Acheteur France";
                    case 3:return "Acheteur Non France";
                    case 4:return "Brestation Et Service";
                    case 5:return "Comple Courant";
                    case 6:return "默认合作商";
                }
            }},
            {field: 'number', title: "编号", align:"center"},
            {field: 'simpleName', title: "简名", align:"center", width: 200, templet : function( d){
                return "<div style=\"overflow:hidden;white-space:nowrap;text-overflow:ellipsis;\">" + d.simpleName + "</div>";
            }},
            {field: 'name', title: "名称", align:"center", width: 300, templet : function( d){
                return "<div style=\"overflow:hidden;white-space:nowrap;text-overflow:ellipsis;\">" + d.name + "</div>";
            }},
            {field: 'addr', title: "地址", align:"center", templet: function ( d) {
                return "<div style=\"overflow:hidden;white-space:nowrap;text-overflow:ellipsis;\">" + d.addr + "</div>";
            }},
            {field: 'emailCode', title: "邮编", align:"center"},
            {field: 'city', title: "城市", align:"center"},
            {field: 'country', title: "国家", align:"center"},
            {field: 'createTime', title: '添加时间', align:'center', templet : function( d){
                return util.toDateString( d.createTime);
            }},
            {title: 'État', templet:'#listBar',fixed: "right", align:"center"}
        ]],
        response : {
            statusCode: 200 //规定成功的状态码，默认：0
        },
        parseData : function( res){ //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.length, //解析数据长度
                "data": res.data //解析数据列表
            };
        }
    });
    var maxHeight = $(window).height();

    //添加/修改/详细查看（readonly=true）
    function addOrUpdateOrDetail( data, readonly){
        var index = layui.layer.open({
            title : "添加",
            type : 2,
            area: [600 + 'px', (maxHeight < 500 ? maxHeight-10 : 500) + 'px'],
            shade: 0.4,
            btnAlign: 'r',
            id: "cooperative", //设定一个id，防止重复弹出
            content : "formData.html",
            success : function(){
                var body = layui.layer.getChildFrame( 'body', index);
                if( data){
                    body.find( "input[name=id]").val( data.id).attr( "readonly", readonly);//标识是修改还是新增
                    body.find( "select[name=type]").val( data.type).attr( "readonly", readonly);
                    body.find( "input[name=number]").val( data.number);
                    body.find( "input[name=showNumber]").val( data.showNumber);
                    body.find( "input[name=simpleName]").val( data.simpleName);
                    body.find( "input[name=name]").val( data.name).attr( "readonly", readonly);
                    body.find( "input[name=addr]").val( data.addr).attr( "readonly", readonly);
                    body.find( "input[name=emailCode]").val( data.emailCode).attr( "readonly", readonly);
                    body.find( "input[name=city]").val( data.city).attr( "readonly", readonly);
                    body.find( "input[name=country]").val( data.country).attr( "readonly", readonly);
                    if( readonly){
                        body.find( "#submitDiv").hide();
                    }
                    form.render();
                }
                setTimeout( function(){
                    layui.layer.tips('点击此处返回数据列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500);
            }
        });
    }

    //添加
    $(".add_btn").click(function(){
        var index = layui.layer.open({
            title : "添加",
            type : 2,
            area: [600 + 'px', 300 + 'px'],
            shade: 0.4,
            btnAlign: 'r',
            id: "formData_simple", //设定一个id，防止重复弹出
            content : "formData_simple.html",
            success : function(){
                var body = layui.layer.getChildFrame( 'body', index);
                setTimeout( function(){
                    layui.layer.tips('点击此处返回数据列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500);
            }
        });
    });

    //搜索
    $("input[name=number]").on("input propertychange",function(e){
        search();
    });
    $("input[name=name]").on("input propertychange",function(e){
        search();
    });
    $("input[name=simpleName]").on("input propertychange",function(e){
        search();
    });

    function search(){
        var number = $("input[name=number]").val();
        var name = $("input[name=name]").val();
        var simpleName = $("input[name=simpleName]").val();
        table.reload("listTable",{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                "number": number,
                "name": name,
                "simpleName": simpleName
            }
        })
    }

    //删除
    function del( id){
        layer.confirm('确定删除选中的数据项？', {icon: 3, title: '提示信息'}, function (index) {
            $.post("/cooperative/del",{
                "id" : id
            },function( res){
                if( res.code == 200){
                    tableIns.reload();
                    layer.close( index);
                    top.layer.msg( res.msg);
                }else{
                    top.layer.close( index);
                    top.layer.msg( res.msg);
                }
            });
        });
    }

    //列表操作
    table.on('tool(list)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            addOrUpdateOrDetail( data, false);
        }else if(layEvent === 'look'){ //预览
            addOrUpdateOrDetail( data, true);
        }else if(layEvent === 'del'){
            del( data.id);
        }
    });
});
